/*
 * 装备制作
 */

var status = 0;
var MakeEquipList = new Array(
        new Array(1082488, 4033602, 1),
        new Array(1082488, 4033603, 1),
        new Array(1082488, 4033604, 1),
        new Array(1082488, 4033605, 1),
        new Array(1082489, 4033606, 1),
        new Array(1082489, 4033607, 1),
        new Array(1082489, 4033608, 1),
        new Array(1082489, 4033609, 1)
        );
var SelectEquipList = new Array();
var itemidSelect;

function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (status == 0 && mode == 0) {
        cm.dispose();
        return;
    }
    if (mode == 1) {
        status++;
    } else {
        status--;
    }
    if (status == 0) {
        var text = "你想通过碎片來兑换手套吗？\r\n\r\n";
        text += "#r#e#h0##k#n，你想兑换哪个手套呢？！\r\n#b"
        for (var i = 0; i < MakeEquipList.length; i++) {
            if (i == 0) {
                text += "#L" + MakeEquipList[i][0] + "# 制作 #i" + MakeEquipList[i][0] + "# #t" + MakeEquipList[i][0] + "#\r\n";
            }
            if (i != 0 && (MakeEquipList[i][0] != MakeEquipList[i - 1][0])) {
                text += "#L" + MakeEquipList[i][0] + "# 制作 #i" + MakeEquipList[i][0] + "# #t" + MakeEquipList[i][0] + "#\r\n";
            }
        }
        cm.sendSimple(text);
    } else if (status == 1) {
        itemidSelect = selection;
        for (var i = 0; i < MakeEquipList.length; i++) {
            if (MakeEquipList[i][0] == itemidSelect) {
                SelectEquipList.push(MakeEquipList[i]);
            }
        }
        var text = "#e#d - #r#z" + itemidSelect + "##d所需要的材料\r\n - (移动到红色处可查看原始属性)\r\n\r\n#n";
        for (var i = 0; i < SelectEquipList.length; i++) {
            if (cm.getItemQuantity(SelectEquipList[i][1]) >= SelectEquipList[i][2]) {
                text += "#g " + FormatString(" ", 20, cm.getItemName(SelectEquipList[i][1])) + "\t\t" + cm.getItemQuantity(SelectEquipList[i][1]) + " / " + SelectEquipList[i][2] + " 个\r\n#k";
            } else {
                text += "#r " + FormatString(" ", 20, cm.getItemName(SelectEquipList[i][1])) + "\t\t" + cm.getItemQuantity(SelectEquipList[i][1]) + " / " + SelectEquipList[i][2] + " 个\r\n#k";
            }
        }
        cm.sendNext(text);
    } else if (status == 2) {
        if (cm.getSpace(1) < 1 && cm.getSpace(2) < 1 && cm.getSpace(3) < 1 && cm.getSpace(4) < 1 && cm.getSpace(5) < 1) {
            cm.sendOk("请确保您的所有背包栏是否有一个以上的空格。");
            cm.dispose();
            return;
        }
        var notHaveItem = false;
        var NothaveItemList = new Array();
        for (var i = 0; i < SelectEquipList.length; i++) {
            if (!cm.haveItem(SelectEquipList[i][1], SelectEquipList[i][2])) {
                notHaveItem = true;
                NothaveItemList.push(SelectEquipList[i]);
            }
        }
        if (notHaveItem) {//如果没有足够的道具
            var text = "#d#e请确保您的背包是否有足够的制作道具！#n\r\n #r- 缺少下列道具：\r\n";
            for (var i = 0; i < NothaveItemList.length; i++) {
                text += "#z" + NothaveItemList[i][1] + "#  目前拥有:(" + cm.getItemQuantity(NothaveItemList[i][1]) + ") /  " + NothaveItemList[i][2] + "个\r\n"
            }
            cm.sendOk(text);
            cm.dispose();
            return;
        } else {
            MakeEquip(itemidSelect);
            for (var i = 0; i < SelectEquipList.length; i++) {
                cm.gainItem(SelectEquipList[i][1], -SelectEquipList[i][2]);//扣除道具
            }
            cm.sendOk("恭喜你！兌換成功了！");
            cm.worldSpouseMessage(0x23, "『次元手套』 : 玩家 " + cm.getPlayer().getName() + " 成功兌換了次元手套。");
            cm.dispose();
        }
    }
}

function MakeEquip(itemid) {//制作装备
        cm.gainItem(itemid, 1);
}

function FormatString(c, length, content) {
    var str = "";
    var cs = "";
    if (content.length > length) {
        str = content;
    } else {
        for (var j = 0; j < length - content.getBytes("GB2312").length; j++) {
            cs = cs + c;
        }
    }
    str = content + cs;
    return str;
}
